<?php
class couponController extends RPHP_Controller
{

    //权限前缀
    protected $_pvprefix = "coupon";

    public function clist()
    {
        $priv  = array($this->_pvprefix . "_ls");
        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $para = array();
            if (intval($_GET['page']) > 0) {
                $page = intval($_GET['page']);
            } else {
                $page = 1;
            }

            if (!empty($_GET['keyword'])) {
                $para['keyword'] = addslashes(stripslashes(trim($_GET['keyword'])));
            }

            if ($_GET['member_level'] != "") {
                $para['member_level'] = intval($_GET['member_level']);
            }

            if ($_GET['pid'] != "") {
                $para['pid'] = intval($_GET['pid']);
            }

            if ($_GET['stat'] != "") {
                $para['stat'] = intval($_GET['stat']);
            }
            
           if (isset($_GET['type']) && $_GET['type'] !='') {
                $para['type'] = intval($_GET['type']);
            }

            $this->view->data = $model->getCouponList($para, $page);
            //print_r($this->view->data);die;

            //获取页码
            if ($this->view->data['maxpage'] > 1) {
                $this->view->data['spage'] = $model->fun->spage($this->view->data['page'], $this->view->data['maxpage']);
            }

            $this->view->auth = $auth;
            $this->display();

        }

    }
    
    //优惠券用户使用记录
    public function coupon_use_detail()
    {
        $priv  = array($this->_pvprefix . "_ls");
        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $para = array();
            if (intval($_GET['page']) > 0) {
                $page = intval($_GET['page']);
            } else {
                $page = 1;
            }
        
           
          if (!empty($_GET['keyword'])) {
                $para['keyword'] = addslashes(stripslashes(trim($_GET['keyword'])));
            }
            $this->view->data = $model->getCouponUseDetail($para, $page);
            //print_r($this->view->data);die;
        
            //获取页码
            if ($this->view->data['maxpage'] > 1) {
                $this->view->data['spage'] = $model->fun->spage($this->view->data['page'], $this->view->data['maxpage']);
            }
        
            $this->view->auth = $auth;
            $this->display();
        
        }
    }
    
    public function dlist()
    {
        $priv  = array($this->_pvprefix . "_dls");
        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $para = array();
            if (intval($_GET['page']) > 0) {
                $page = intval($_GET['page']);
            } else {
                $page = 1;
            }
    
            if (!empty($_GET['keyword'])) {
                $para['keyword'] = addslashes(stripslashes(trim($_GET['keyword'])));
            }
    
            if ($_GET['member_level'] != "") {
                $para['member_level'] = intval($_GET['member_level']);
            }
    
            if ($_GET['pid'] != "") {
                $para['pid'] = intval($_GET['pid']);
            }
    
            if ($_GET['stat'] != "") {
                $para['stat'] = intval($_GET['stat']);
            }
    
            if (isset($_GET['type']) && $_GET['type'] !='') {
                $para['type'] = intval($_GET['type']);
            }
    
            $this->view->data = $model->getCouponList($para, $page);
            //print_r($this->view->data);die;
    
            //获取页码
            if ($this->view->data['maxpage'] > 1) {
                $this->view->data['spage'] = $model->fun->spage($this->view->data['page'], $this->view->data['maxpage']);
            }
    
            $this->view->auth = $auth;
            $this->display();
    
        }
    
    }
    //代金券
    
    public function voucher()
    {
        $priv  = array($this->_pvprefix . "_vc");
        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth         = $auth;
            $this->display();
    
        }
    
    }
    
    public function send_coupon()
    {
        $priv  = array($this->_pvprefix . "_vc");
        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth         = $auth;
            $this->display();
    
        }
    
    }
    //新增
    public function add()
    {

        $priv = array($this->_pvprefix . "_ad");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {

            $this->view->auth  = $auth;
            $this->display();

        }

    }

    //编辑
    public function edit()
    {
        $priv = array($this->_pvprefix . "_ed");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $dis_id = intval($_GET['dis_id']);
            if ($dis_id > 0) {
                $data = $model->getSingleCoupon($dis_id);
            }

            $this->view->data = $data;
            $this->view->auth = $auth;
            $this->display();

        }

    }

    // 保存
    public function save()
    {
        $priv = array($this->_pvprefix . "_ls");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            
            $this->view->code = 900;
            $this->view->auth = $auth;

            if ($_POST['act'] == "add" && $model->checkPriv($this->_pvprefix . "_ad", false, false)) {
                $this->view->success = "新增优惠券成功";
                $this->view->link    = array(
                    array('name' => '继续新增优惠券', 'url' => '/coupon/add'),
                    array('name' => '返回优惠券列表', 'url' => '/coupon/clist?type=0'),
                );

                $this->view->code = $model->addCoupon($_POST);
            }
            elseif ($_POST['act'] == "edit" && $model->checkPriv($this->_pvprefix . "_ed", false, false)) {
                $this->view->success = "编辑优惠券成功";
                $this->view->link    = array(
                    array('name' => '返回优惠券列表', 'url' => '/coupon/clist?type=0'),
                );
                
                $this->view->code = $model->editCoupon($_POST);
            }
            elseif ($_POST['act'] == "send_voucher" && $model->checkPriv($this->_pvprefix . "_vc", false, false)) {
                $this->view->success = "发放代金券成功";
                $this->view->link    = array(
                    array('name' => '继续发放', 'url' => '/coupon/voucher'),
                );
                
                $this->view->code = $model->sendVoucher($_POST);
            }
            elseif ($_POST['act'] == "send_coupon") {
                $this->view->success = "发放优惠券成功";
                $this->view->link    = array(
                    array('name' => '继续发放', 'url' => '/coupon/send_coupon?dis_id='.$_POST['dis_id']),
                );
            //print_r($_POST);die;
                $this->view->code = $model->sendCoupon($_POST);
            }
            $this->displaydo();

        }
    }
    
    //AJAX操作
    public function ajax()
    {
        $priv = array($this->_pvprefix . "_ls");
    
        $model        = $this->model;
        $auth         = $model->checkPriv($priv);
        $data['code'] = 900;
        if ($auth) {
            if ($_POST['act'] == 'ajax_member') {
                $all_member = $model->getAllMember($_POST);
                if ($all_member) {
                    $data['code']       = 200;
                    $data['all_member'] = $all_member;
                }
            }elseif($_POST['act'] == 'del'){
               
                $data = $model->delVoucher($_POST['dis_id']);
                
            }elseif($_POST['act'] == 'user_coupon_del'){
               
                $data = $model->delDiscountCoupon($_POST['coupon_id']);
                
            }  
        }
        echo json_encode($data);
    
    }

}
